//
//  GoodsDetailViewController.m
//  YunGou
//
//  Created by macbook on 16/5/19.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "GoodsDetailViewController.h"
#import "ImageTextDetailController.h"
#import "ShowOrderViewController.h"
#import "ParttakeViewController.h"
#import "CalculateWayViewController.h"
#import "GoodsDetailCountDownCell.h"
#import "GoodsDetailPartTakeCell.h"
#import "GoodsDetailHasAnnounceCell.h"
#import "GoodsDetailGoodsInfoCell.h"
#import "SDCycleScrollView.h"
#import "GoodsDetailBottomView.h"
#import "GoodsDetailBottomView1.h"
#import "NSObject+Tools.h"
#import "LoginViewController.h"
#import "NSString+Frame.h"
#import <MJRefresh.h>

@interface GoodsDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *imgArray;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet SDCycleScrollView *topScrollView;
@property (nonatomic,strong) GoodsDetailBottomView *buyBottomView;
@property (nonatomic,strong) GoodsDetailBottomView1 *donotAnnounceBottomView;
//购物车数量
@property (nonatomic,assign) NSInteger shopCartCount;
@end

@implementation GoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = @[@"图文详情",@"晒单分享",@"所有参与记录"];
    _imgArray = @[@"spxq_twxq",@"spxq_sdfx",@"spxq_cyjl"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.tableView.mj_header endRefreshing];
    }];
    
    NSMutableArray *picArr = [NSMutableArray array];
    for (NSString *pic in _productModel.picarr) {
        [picArr addObject:[ImageUrl stringByAppendingString:pic]];
    }
    
    _topScrollView.imageURLStringsGroup = picArr;
    _topScrollView.currentPageDotColor = ThemeColor;
    
    if (_detailType == DonotAnnounce) {
        if (_donotAnnounceBottomView) {
            [_donotAnnounceBottomView removeFromSuperview];
            _donotAnnounceBottomView = nil;
        }
        if (!_buyBottomView) {
            [self.bottomView addSubview:self.buyBottomView];
            if (IsLogin) {
                [self loadShopCartCount];
            }
        }
    }else{
        if (_buyBottomView) {
            [_buyBottomView removeFromSuperview];
            _buyBottomView = nil;
        }
        if (!_donotAnnounceBottomView) {
            [self.bottomView addSubview:self.donotAnnounceBottomView];
        }
    }
}

- (void)loadShopCartCount{
    [NetWorkTool shopCartListWithSuceess:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            NSArray *shopCartArray = object[@"data"];
            _shopCartCount = shopCartArray.count;
            if (_shopCartCount != 0) {
                _buyBottomView.goodsCartNum.hidden = NO;
                _buyBottomView.goodsCartNum.text = [NSString stringWithFormat:@"%ld",_shopCartCount];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma -mark about tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __block UITableViewCell *baseCell = nil;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            GoodsDetailGoodsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
            cell.model = _productModel;
            baseCell = cell;
        }else if (indexPath.row == 1){
            if (_detailType == NowAnnounce) {//正在揭晓
                GoodsDetailCountDownCell *cell = [tableView dequeueReusableCellWithIdentifier:@"countdownCell"];
                cell.model = _productModel;
                cell.countdownBlock = ^{
                    CalculateWayViewController *calculateWayVC = SB(@"Common", @"CalculateWayVC");
                    calculateWayVC.goodsId = _productModel.Id;
                    [self.navigationController pushViewController:calculateWayVC animated:YES];
                };
                
                cell.endCountDownBlock = ^{
                    _detailType = HasAnnounce;
                    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
                };
                baseCell = cell;
            }else if (_detailType == DonotAnnounce){//未揭晓
                GoodsDetailPartTakeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"partakeCell"];
                cell.model = _productModel;
                baseCell = cell;
            }else{//已经揭晓
                GoodsDetailHasAnnounceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"winInfoCell"];
                cell.model = _productModel;
                cell.block = ^{
                    CalculateWayViewController *calculateWayVC = SB(@"Common", @"CalculateWayVC");
                    calculateWayVC.goodsId = _productModel.Id;
                    [self.navigationController pushViewController:calculateWayVC animated:YES];
                };
                baseCell = cell;
            }
        }
    }else{
        baseCell = [tableView dequeueReusableCellWithIdentifier:@"imgTitleCell"];
        UIImageView *imgView = [baseCell.contentView viewWithTag:1];
        imgView.image = [UIImage imageNamed:_imgArray[indexPath.row]];
        
        UILabel *titLabel = [baseCell.contentView viewWithTag:2];
        titLabel.text = _titleArray[indexPath.row];
    }
    return baseCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CGFloat height = [_productModel.title heightWithFont:[UIFont systemFontOfSize:17] withinWidth:ScreenWidth - 32];
            return 75 - 41 + height;
        }else{
            if (_detailType == HasAnnounce) {
                return 130;
            }
            return 65;
        }
    }else{
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            ImageTextDetailController *imageDetailVC = [[ImageTextDetailController alloc] init];
            imageDetailVC.productModel = _productModel;
            [self.navigationController pushViewController:imageDetailVC animated:YES];
        }else if (indexPath.row == 1){
            ShowOrderViewController *showOrderVC = SB(@"Common", @"ShowOrderVC");
            showOrderVC.goodsId = _productModel.Id;
            showOrderVC.isShare = YES;
            showOrderVC.title = @"晒单分享";
            [self.navigationController pushViewController:showOrderVC animated:YES];
        }else{
            ParttakeViewController *parttakeVC = SB(@"Common", @"ParttakeVC");
            parttakeVC.goodsId = _productModel.Id;
            [self.navigationController pushViewController:parttakeVC animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01f;
    }return 10.0f;
}

#pragma -mark 懒加载相关
- (GoodsDetailBottomView *)buyBottomView{
    if (!_buyBottomView) {
        _buyBottomView = [GoodsDetailBottomView nibInitializtion];
        _buyBottomView.frame = self.bottomView.bounds;
        if (!IsLogin) {
            _buyBottomView.goodsCartNum.hidden = YES;
        }
        __weak typeof(self) ws = self;
        _buyBottomView.bottomBlock = ^(NSInteger tag){
            if (!IsLogin) {
                LoginViewController *loginVC = SB(@"Login", @"LoginVC");
                [ws.navigationController pushViewController:loginVC animated:YES];
                return;
            }
            if (tag == 1) {//立即购买的时候跳转到购物车界面 在购物车进行结算
                [NetWorkTool addShopCartWithGoodsId:ws.productModel.Id success:^(id object) {
                    if ([object[@"code"] integerValue] == 1) {
                        ws.tabBarController.selectedIndex = 3;
                        [ws.navigationController popToRootViewControllerAnimated:NO];
                    }
                } failure:^(NSError *error) {
                }];
            }else if (tag == 2) {//添加到购物车
                [NetWorkTool addShopCartWithGoodsId:ws.productModel.Id success:^(id object) {
                    if ([object[@"code"] integerValue] == 1) {
                        [MBProgressHUD showSuccess:@"添加成功"];
                        [ws loadShopCartCount];
                    }
                } failure:^(NSError *error) {
                }];
            }else if (tag == 3){
                ws.tabBarController.selectedIndex = 3;
                [ws.navigationController popToRootViewControllerAnimated:NO];
            }
        };
    }
    return _buyBottomView;
}

- (GoodsDetailBottomView1 *)donotAnnounceBottomView{
    if (!_donotAnnounceBottomView) {
        _donotAnnounceBottomView = [GoodsDetailBottomView1 nibInitializtion];
        _donotAnnounceBottomView.frame = self.bottomView.bounds;
        _donotAnnounceBottomView.nowQiShu.text = [NSString stringWithFormat:@"第%@期正火爆进行中",_productModel.qishu];
        __weak typeof(self) ws = self;
        _donotAnnounceBottomView.block = ^{
            if (!IsLogin) {
                LoginViewController *loginVC = SB(@"Login", @"LoginVC");
                [ws.navigationController pushViewController:loginVC animated:YES];
                return;
            }
            if (ws.donotAnnounceBottomView) {
                [ws.donotAnnounceBottomView removeFromSuperview];
                ws.donotAnnounceBottomView = nil;
                [ws.bottomView addSubview:ws.buyBottomView];
                [ws loadShopCartCount];
            }
            _detailType = DonotAnnounce;
            [ws.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        };
    }
    return _donotAnnounceBottomView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
